package com.cl.mvc.util;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

public class ClassGeneratorUtil {
    public static void main(String[] args) {
        generate("/Users/chenlong/mine/jd/ks-service/ks-entity/src/main/java/com/cl/ks","/Users/chenlong/mine/jd/ks-service/ks-service-jpa/src/main/java/com/cl/ks","com.cl.ks");
    }

    /**
     * @param filePath    在该路径下根据建好的entity生成repository、service、controller
     * @param packageName 包位置
     */
    public static void generate(String filePath,String outputPath, String packageName) {
        String entityPath = filePath + "/entity/";
        if (!FileUtil.exist(entityPath)) {
            throw new RuntimeException("缺少entity");
        }
        String repositoryPath = outputPath + "/repository/";
        String servicePath = outputPath + "/service/";
        String controllerPath = outputPath + "/controller/";
        File repositoryDir = new File(repositoryPath);
        File serviceDir = new File(servicePath);
        File controllerDir = new File(controllerPath);
        if (!repositoryDir.exists()) {
            repositoryDir.mkdirs();
        }
        if (!serviceDir.exists()) {
            serviceDir.mkdirs();
        }
        if (!controllerDir.exists()) {
            controllerDir.mkdirs();
        }
        List<String> entityNames = FileUtil.listFileNames(entityPath);
        entityNames.forEach(entityName -> {
            if (!FileUtil.readUtf8String(entityPath + entityName).contains("BaseEntity")) {
                return;
            }
            entityName = entityName.replace(".java", "");
            File repositoryFile = new File(repositoryPath + entityName + "Repository.java");
            File serviceFile = new File(servicePath + entityName + "Service.java");
            File controllerFile = new File(controllerPath + entityName + "Controller.java");
            generateFile(repositoryFile, getRepository(packageName, entityName));
            generateFile(serviceFile, getService(packageName, entityName));
            generateFile(controllerFile, getController(packageName, entityName));
        });
    }

    public static void generateFile(File file, String content) {
        if (file.exists()) {
            return;
        }
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getRepository(String packageName, String entity) {
        return getRepositoryTemplate().replace("#{packageName}", packageName).replace("#{entity}", entity);
    }

    public static String getService(String packageName, String entity) {
        return getServiceTemplate().replace("#{packageName}", packageName).replace("#{entity}", entity);
    }

    public static String getController(String packageName, String entity) {
        return getControllerTemplate().replace("#{packageName}", packageName).replace("#{entity}", entity);
    }

    public static String getRepositoryTemplate() {
        return "package #{packageName}.repository;\n" +
                "\n" +
                "import com.cl.mvc.repository.BaseRepository;\n" +
                "import #{packageName}.entity.#{entity};\n" +
                "import org.springframework.stereotype.Repository;\n" +
                "\n" +
                "@Repository\n" +
                "public interface #{entity}Repository extends BaseRepository<#{entity}> {}";
    }


    public static String getServiceTemplate() {
        return "package #{packageName}.service;\n" +
                "\n" +
                "import com.cl.mvc.service.BaseService;\n" +
                "import #{packageName}.entity.#{entity};\n" +
                "import #{packageName}.repository.#{entity}Repository;\n" +
                "import org.springframework.stereotype.Service;\n" +
                "\n" +
                "@Service\n" +
                "public class #{entity}Service extends BaseService<#{entity}> {\n" +
                "    protected #{entity}Service(#{entity}Repository #{entity}Repository) {\n" +
                "        super(#{entity}Repository);\n" +
                "    }\n" +
                "}\n";
    }

    public static String getControllerTemplate() {
        return "package #{packageName}.controller;\n" +
                "\n" +
                "import com.cl.mvc.controller.BaseController;\n" +
                "import #{packageName}.entity.#{entity};\n" +
                "import #{packageName}.service.#{entity}Service;\n" +
                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                "import org.springframework.web.bind.annotation.RestController;\n" +
                "\n" +
                "@RestController\n" +
                "@RequestMapping(\"#{entity}\")\n" +
                "public class #{entity}Controller extends BaseController<#{entity}> {\n" +
                "    public #{entity}Controller(#{entity}Service service) {\n" +
                "        super(service);\n" +
                "    }\n" +
                "}\n";
    }
}
